package cn.kust.tarvelService.controller;


import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.kust.tarvelService.pojo.Article;
import cn.kust.tarvelService.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 游记管理表 前端控制器
 * </p>
 *
 * @author mq
 * @since 2022-07-26
 */
@Api(tags = "游记管理")
@RestController
@RequestMapping("/travelService/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping("/addArticle")
    public ResponseResult addCategory(@RequestBody Article article){
       articleService.save(article);
        return ResponseResult.success();
    }

    @GetMapping("/getArticle/{articleId}")
    public ResponseResult addCategory(@PathVariable Long articleId){
        Article article = articleService.getByIdArticle(articleId);
        System.out.printf("article文章信息为====>"+article);
        return ResponseResult.success().setData(article);
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("getArticleList/{current}/{limit}")
    public ResponseResult getCourseList(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current") Long current,
                                @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit){

        Page<Article> articlePage= new Page<>(current,limit);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("views");
        articleService.page(articlePage,queryWrapper);
        long total=articlePage.getTotal(); //记录总页数
        List<Article> records = articlePage.getRecords();//数据list集合
        return ResponseResult.success().setData(articlePage);
    }

    //修改文章信息
    @PostMapping("/updateArticleInfo")
    public ResponseResult updateCourseInfo(@RequestBody Article articleInfo) {
        articleService.updateArticle(articleInfo);
        return ResponseResult.success();
    }

    //删除文章
    @DeleteMapping("{articleID}")
    public ResponseResult deleteCourse(@PathVariable Long articleID) {
        articleService.removeById(articleID);
        return ResponseResult.success();
    }
}

